package com.example.test.service.impl;

import com.example.test.Dao.BraendDao;
import com.example.test.Dao.TypeDao;
import com.example.test.bean.BraendType;
import com.example.test.bean.GoodType;
import com.example.test.common.model.PageModel;
import com.example.test.common.model.R;
import com.example.test.mapper.GoodTypeMapper;
import com.example.test.service.BraendTypeService;
import com.example.test.service.GoodTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Gt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 基础分类表 服务实现类
 * </p>
 *
 * @author dz
 * @since 2022-04-10
 */
@Service
public class GoodTypeServiceImpl extends ServiceImpl<GoodTypeMapper, GoodType> implements GoodTypeService {

    @Resource
    private TypeDao typeDao;
    private final Logger LOGGER = LoggerFactory.getLogger(GoodTypeService.class);


    //根据keyword查询
    public R getTypeByKeyword(String keyword, PageModel pageModel){
        try {
            Long current = pageModel.getCurrent();
            Long size = pageModel.getSize();
            Long offset = (current - 1) * size;
            List<GoodType> typeList = typeDao.getByKeyword(keyword,size,offset);
            return R.success(typeList);
        } catch (Exception e) {
            LOGGER.error("", e);
            return R.error("查询分类失败:" + e.getMessage());
        }
    }

    //根据id查询
    public R getTypeById(Long typeId){
        try {
            GoodType goodType = typeDao.getById(typeId);
            if(goodType != null){
                return R.success(goodType);
            }
            return R.error("查询失败");
        } catch (Exception e) {
            LOGGER.error("", e);
            return R.error("查询分类失败:" + e.getMessage());
        }
    }

    //新增分类
    public R addType(String typeName){
        try {
            GoodType newType = new GoodType();
            //查询是否有对应的品牌，如果没有，则新增
            if(typeName != null && typeName.length()>1){
                Long typeId = typeDao.getIdByName(typeName);
                if(typeId == null){
                    newType.setDeleted(false);
                    newType.setTypeName(typeName);
                    int addType = typeDao.insert(newType);
                    if(addType == 0){
                        return R.error("新建分类失败");
                    }
                    return R.success("插入成功"+addType);
                }
                return R.error("已有该分类");

            }else{return R.error("typeName为空");}
        }catch(Exception e) {
            LOGGER.error("", e);
            return R.error("新增分类失败:" + e.getMessage());
        }
    }

    //更新分类
    public R updateType(GoodType goodType){
        try {
            Long typeId = goodType.getTypeId();
            String typeName = goodType.getTypeName();
            if(typeId != null && typeName.length()>1){
                //更新数据库
                int updatenum = typeDao.updateById(goodType);
                if(updatenum == 0){
                    return R.error("更新分类失败：updatenum为0");
                }
                return R.success("更新分类成功:"+ typeName);
            }else{return R.error("typeId及typeName不能为空");}
        }catch(Exception e) {
            LOGGER.error("", e);
            return R.error("更新分类失败:" + e.getMessage());
        }
    }

    //删除分类
    public R deleteType(Long typeId){
        try {
            GoodType goodType = new GoodType();
            //设置Type
            goodType.setTypeId(typeId);
            goodType.setDeleted(true);
            //更新数据库
            int deletenum = typeDao.updateById(goodType);
            if(deletenum == 0){
                return R.error("删除分类失败：deletenum为0");
            }
            return R.success("删除分类成功:"+ typeId);
        }catch(Exception e) {
            LOGGER.error("", e);
            return R.error("删除分类失败:" + e.getMessage());
        }
    }
}
